/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.boxin.ims.modules.wechat.entity.ScoreRule;
import com.boxin.ims.modules.wechat.dao.ScoreRuleDao;

/**
 * 积分规则Service
 * @author haoziyang
 * @version 2014-12-30
 */
@Component
@Transactional(readOnly = true)
public class ScoreRuleService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(ScoreRuleService.class);
	
	@Autowired
	private ScoreRuleDao scoreRuleDao;
	
	public ScoreRule get(Long id) {
		return scoreRuleDao.findOne(id);
	}
	
	public Page<ScoreRule> find(Page<ScoreRule> page, ScoreRule scoreRule) {
		DetachedCriteria dc = scoreRuleDao.createDetachedCriteria();
		if (scoreRule.getUser()!=null && scoreRule.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", scoreRule.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(scoreRule.getName())){
			dc.add(Restrictions.like("name", "%"+scoreRule.getName()+"%"));
		}
		dc.addOrder(Order.desc("id"));
		return scoreRuleDao.find(page, dc);
	}
	/**
	 * 查询积分规则。
	 * @author ziyang
	 * @param type
	 * @param number
	 * @return
	 */
	public ScoreRule findcbRule(String type){
	
		DetachedCriteria dc = scoreRuleDao.createDetachedCriteria();
	
			dc.add(Restrictions.eq("type", type));
		dc.addOrder(Order.desc("id"));
		return scoreRuleDao.find(dc).get(0);
	}
	
	@Transactional(readOnly = false)
	public void save(ScoreRule scoreRule) {
		scoreRuleDao.save(scoreRule);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		scoreRuleDao.deleteById(id);
	}
	
}
